package Server.ServerCore.Controller;

import Server.ServerCore.Model.Player.PlayerInstance;
import Server.ServerCore.Model.vo.CardResultStatusVO;
import Server.ServerCore.Model.vo.CardVO;
import Server.ServerCore.Repository.ChipsServiceImpl;
import Server.ServerCore.Service.DataPacks.packRequest.Spush_requst;
import Server.ServerCore.Service.DataPacks.packRespose.Chips_reback_Response;
import Server.ServerCore.Service.DataPacks.packRespose.Spush_response;
import Server.ServerCore.Service.DataPacks.packRespose.bust_response;
import Server.ServerCore.Service.Logic.HandleController;
import Server.ServerCore.Service.Logic.RegisterPack;
import Server.ServerCore.Service.service.PlayerManagerService;
import javolution.util.FastList;
import org.jboss.netty.buffer.ChannelBuffer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;

/**
 * @author :石头哥哥<br/>
 *         Project:21Server
 *         Date: 13-4-1
 *         Time: 下午9:58
 *
 *         SpushController=25 分牌
 *         分牌：玩家前两张牌为对子时可以要求分牌。
 *         一门最多可以分成两门
 *
 *         分牌后结算，没有对分的牌操作
 *
 *         分牌bug已修复
 */
@Service
public class SpushController extends HandleController {
    private static final long serialVersionUID = 1685827625063989590L;

    /**
     * 数据分解具体处理的函数
     *
     * @param buffer buffer
     * @param player player
     *               temp1.get(0)新牌
     *               temp1.get(1)旧牌
     */
    @Override
    public void DispatchPack(ChannelBuffer buffer, PlayerInstance player) throws Exception {
        Spush_requst spushRequst= (Spush_requst) parseObject(buffer,Spush_requst.class);

        //纸牌位置
        int circleUnitID=spushRequst.getCircleUnitID();

        //找出指定位置的两张牌，确定分牌后的位置，并返回分牌信息
        //0,1,2,3,4,5(庄)  一门牌最多可以分成两门
        //6,7,8,9,10   即是如果circleUnitID上的玩家分牌，那么两张牌的另一张将会在如下位置：
        //如：circleUnitID=0,那么另外一张则在位置6，
        // 即：（0==6）（1=7）（2==8）（3==9）（4==10）对应；确定位置关系
        FastList<CardVO> cardVOFastList= player.getCardVOFastList();
        FastList<CardVO> temp1=new FastList<CardVO>();//找出指定位置的两面值相同的张牌
        for (CardVO cardVO:cardVOFastList){
            if (cardVO.getCircleUnitID()==circleUnitID){
                temp1.add(cardVO);
            }
        }
        //每一幅牌有一个CardResultStatusVO对象保存该牌的状态,cardResultStatusVO原来牌状态保存对象
        //将temp1第一张牌位置设为当前位置对应的位置编号（0==6）（1=7）（2==8）（3==9）（4==10）对应；确定位置关系保存vo，设置位置
        CardResultStatusVO[] cardResultStatusVOFastList=player.getDeckoOfCardsVOFastList();

        CardResultStatusVO cardResultStatusVO=cardResultStatusVOFastList[circleUnitID];
        CardResultStatusVO cardResultStatusVO1=new CardResultStatusVO();
        if (circleUnitID==0){//0-6
            cardResultStatusVO1.setCircleUnitID(6);
            cardResultStatusVO1.setChipsPoints(cardResultStatusVO.getChipsPoints());  //保存和cardResultStatusVO一样的筹码数，如果分牌失败，1：筹码不够；
            cardResultStatusVOFastList[6]=cardResultStatusVO1;
            temp1.get(0).setCircleUnitID(6);
            player.getCircleUnitIDS().add(6);//添加分牌占CircleUnitIDS位置
            cardResultStatusVO.setIsspush(true);
            cardResultStatusVO1.setIsspush(true);

        }else if (circleUnitID==1){///1-7
            cardResultStatusVO1.setCircleUnitID(7);
            cardResultStatusVO1.setChipsPoints(cardResultStatusVO.getChipsPoints());
            cardResultStatusVOFastList[7]=cardResultStatusVO1;
            temp1.get(0).setCircleUnitID(7);
            player.getCircleUnitIDS().add(7);
            cardResultStatusVO.setIsspush(true);
            cardResultStatusVO1.setIsspush(true);
        }else if (circleUnitID==2){//2-8
            cardResultStatusVO1.setCircleUnitID(8);
            cardResultStatusVO1.setChipsPoints(cardResultStatusVO.getChipsPoints());
            cardResultStatusVOFastList[8]=cardResultStatusVO1;
            temp1.get(0).setCircleUnitID(8);
            player.getCircleUnitIDS().add(8);
            cardResultStatusVO.setIsspush(true);
            cardResultStatusVO1.setIsspush(true);

        }else if (circleUnitID==3){//3-9
            cardResultStatusVO1.setCircleUnitID(9);
            cardResultStatusVO1.setChipsPoints(cardResultStatusVO.getChipsPoints());
            cardResultStatusVOFastList[9]=cardResultStatusVO1;
            temp1.get(0).setCircleUnitID(9);//
            player.getCircleUnitIDS().add(9);
            cardResultStatusVO.setIsspush(true);
            cardResultStatusVO1.setIsspush(true);
        }else {//4-10
            cardResultStatusVO1.setCircleUnitID(10);
            cardResultStatusVO1.setChipsPoints(cardResultStatusVO.getChipsPoints());
            cardResultStatusVOFastList[10]=cardResultStatusVO1;
            temp1.get(0).setCircleUnitID(10);
            player.getCircleUnitIDS().add(10);
            cardResultStatusVO.setIsspush(true);//分牌后如果2张且21点，不算天生21点
            cardResultStatusVO1.setIsspush(true);
        }
        Collections.sort(player.getCircleUnitIDS());

        //判定是否是A，是A，默认当前程序执行过程，否则指向分牌的两张(旧，新)都会被指同时叫牌；
         if (temp1.get(1).getCardType().getName().equalsIgnoreCase("A")){
             //发送旧牌位置信息
             CardVO cardVO2=new CardVO();
             Spush_response spushResponse2=new Spush_response();
             cardVO2.setCircleUnitID(circleUnitID);
             spushResponse2.setCircleUnitID(cardVO2.getCircleUnitID());
             spushResponse2.setCard_colour(cardVO2.getCardcolour());
             spushResponse2.setCard(cardVO2.getCardType().getID());
             player.DirectSend(spushResponse2.getJsonData(2,5));

             //设定该铺牌的点数
//             PlayerManagerService.setPlayerPoints(player,circleUnitID);

             //返回新牌位置6，7，8，9，10
             bust_response bustResponse=new bust_response();
             int nextID=temp1.get(0).getCircleUnitID();
             bustResponse.setNextcircleUnitID(nextID);
             player.DirectSend(bustResponse.getJsonData(2,9));


             //产生新牌属性设置
             CardVO cardVO=new CardVO();
             Spush_response spushResponse=new Spush_response();
             cardVO.setCircleUnitID(temp1.get(0).getCircleUnitID());//设定产生的新牌的位置

             spushResponse.setCircleUnitID(cardVO.getCircleUnitID());
             spushResponse.setCard_colour(cardVO.getCardcolour());
             spushResponse.setCard(cardVO.getCardType().getID());
             player.getCardVOFastList().add(cardVO);
             player.DirectSend(spushResponse.getJsonData(2,5));

             //设定该铺牌的点数
//             PlayerManagerService.setPlayerPoints(player,temp1.get(0).getCircleUnitID());

             //返回nextcircleUnitID    0，1，2，3，4，-1
             bust_response bustResponse2=new bust_response();
             if (player.getNowCrileUnitID()==circleUnitID&&circleUnitID!=5){
                 int ID= PlayerManagerService.getNextcircleUnitID(player, circleUnitID);
                 bustResponse2.setNextcircleUnitID(ID);
                 if (ID!=-1){
                     player.setNowCrileUnitID(ID);
                 }
                 player.DirectSend(bustResponse2.getJsonData(2,9));

             }

         }else {// in additional the card of A,first   point to the old

             //向原来位置发送一张牌
             CardVO cardVO2=new CardVO();
             Spush_response spushResponse2=new Spush_response();

             cardVO2.setCircleUnitID(temp1.get(1).getCircleUnitID());
             spushResponse2.setCircleUnitID(cardVO2.getCircleUnitID());
             spushResponse2.setCard_colour(cardVO2.getCardcolour());
             spushResponse2.setCard(cardVO2.getCardType().getID());

             player.getCardVOFastList().add(cardVO2);
             player.DirectSend(spushResponse2.getJsonData(2,5));

             //设定该铺牌的点数
//             PlayerManagerService.setPlayerPoints(player,temp1.get(1).getCircleUnitID());


             //向新产生的位置发送一张牌
             CardVO cardVO=new CardVO();
             Spush_response spushResponse=new Spush_response();
             cardVO.setCircleUnitID(temp1.get(0).getCircleUnitID());//设定产生的新牌的位置

             spushResponse.setCircleUnitID(cardVO.getCircleUnitID());
             spushResponse.setCard_colour(cardVO.getCardcolour());
             spushResponse.setCard(cardVO.getCardType().getID());

             player.getCardVOFastList().add(cardVO);
             player.DirectSend(spushResponse.getJsonData(2,5));

             //设定该铺牌的点数
//             PlayerManagerService.setPlayerPoints(player,temp1.get(0).getCircleUnitID());




             //返回新位置
             bust_response bustResponse2=new bust_response();
             int ID=temp1.get(0).getCircleUnitID();

             bustResponse2.setNextcircleUnitID(ID);
             if (ID!=-1) player.setNowCrileUnitID(ID);
             player.DirectSend(bustResponse2.getJsonData(2,9));
         }


        //分牌 减少筹码数并更新
        double lastChips=player.getBargainingChip()-cardResultStatusVO1.getChipsPoints();
        player.setBargainingChip(lastChips);

//        System.out.println("分牌同步"+ player.getBargainingChip());

        //同步筹码数据
        Chips_reback_Response rebackResponse=new Chips_reback_Response();
        rebackResponse.setChips(player.getBargainingChip());
        player.DirectSend(rebackResponse.getJsonData(2,7));

        //更新数据库
        PlayerManagerService.update(player.getGameRole(),player.getBargainingChip());

        player.atomicInteger.incrementAndGet();

        temp1.clear();
    }

    /**
     * 通过spring产生处理数据的controller，储存在数组中
     */
    @Override
    public void InitiController() {
        RegisterPack.GAME_CONTROLLERS[25]=this; //To change body of implemented methods use File | Settings | File Templates.
    }
}
